<?php
class Mageex_AdvanceSeo_Block_Review_Helper extends Mage_Review_Block_Helper
{
	protected $_availableTemplates = array(
        'default' => 'review/helper/summary.phtml',
        'short'   => 'review/helper/summary_short.phtml'
    );

    public function getSummaryHtml($product, $templateType, $displayIfNoReviews)
    {
        // pick template among available
        if (empty($this->_availableTemplates[$templateType])) {
            $templateType = 'default';
        }
        $this->setTemplate($this->_availableTemplates[$templateType]);

        $this->setDisplayIfEmpty($displayIfNoReviews);

        if (!$product->getRatingSummary()) {
            Mage::getModel('review/review')
               ->getEntitySummary($product, Mage::app()->getStore()->getId());
        }
        $this->setProduct($product);

        return $this->toHtml();
    }

    public function getRatingSummary()
    {
        return $this->getProduct()->getRatingSummary()->getRatingSummary();
    }

    public function getReviewsCount()
    {
        return $this->getProduct()->getRatingSummary()->getReviewsCount();
    }

	public function getReviewsUrl()
    {
        if(Mage::helper('advanceseo/config')->isRewriteReview()){
		
             $catName = Mage::getModel('catalog/category')
                        ->load($this->getProduct()->getCategoryId())
                        ->getName();
             $catName = Mage::helper('advanceseo/data')->stringConvert($catName);
             
             $productName = Mage::helper('advanceseo/data')->stringConvert($this->getProduct()->getName());
             
             $urlrewriteModel = Mage::getModel('core/url_rewrite');
			 
             $dataRewrite = array();
             
             $targetPath = 'review/product/list/id/'.$this->getProduct()->getId(); 
			 
             $cfpath = Mage::helper('advanceseo/config')->getPathReview();
			 
             $requestPath = $productName.'/'.$cfpath.Mage::helper('advanceseo/data')->getSuffix();        
              
             $rewriteCollection = $urlrewriteModel->getCollection()
                                  ->addFieldToFilter('target_path', $targetPath);
                                  //->addFieldToFilter('request_path', $requestPath);
             if(count($rewriteCollection)==0){
                $dataRewrite['store_id'] = Mage::app()->getStore()->getId();
                $dataRewrite['id_path'] = 'review/'.$targetPath;
                $dataRewrite['target_path'] = $targetPath;
                $dataRewrite['request_path'] = $requestPath;
                $dataRewrite['is_system'] = 0;
                
                try {
                    $urlrewriteModel->setData($dataRewrite);
                    $urlrewriteModel->save();
                   
                }
                catch (Exception $e) {
						
                }
             }else{
				//If targetpath existed
				//Update core_url_rewrite
		
				foreach($rewriteCollection as $rewrite){
					$idCoreUrl = $rewrite->getId();
					$requestPaths = $rewrite->getRequestPath();
					break;
				}
				if($requestPaths != $requestPath){
					$data['request_path'] = $requestPath;
					$urlrewriteModel->load($idCoreUrl)->addData($data);
					$urlrewriteModel->setId($idCoreUrl)->save();
				}
			 }
             return Mage::getBaseUrl().$requestPath;
        }
        else{
            return Mage::getUrl('review/product/list', array(
	           'id'        => $this->getProduct()->getId(),
	           'category'  => $this->getProduct()->getCategoryId()
	        ));
        }
    }

    /**
     * Add an available template by type
     *
     * It should be called before getSummaryHtml()
     *
     * @param string $type
     * @param string $template
     */
    public function addTemplate($type, $template)
    {
        $this->_availableTemplates[$type] = $template;
    }
}
